package com.qs.entidades;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.ksoap2.SoapEnvelope;

import org.ksoap2.serialization.SoapObject;

import org.ksoap2.serialization.SoapSerializationEnvelope;

import org.ksoap2.transport.HttpTransportSE;
import org.xmlpull.v1.XmlPullParserException;

import android.util.Log;

public class WsLogin
{
	private static final String	METHOD_NAME	= "validarAcceso";
	private static final String	SOAP_ACTION	= "http://quicorp.org/validarAcceso";
	private static final String	NAMESPACE	= "http://quicorp.org/";
	private static final String	URL			= "http://www.quimicasuiza.com/wsmquimovil/service.asmx";

	// private static final String METHOD_NAME = "validarAccesoICA";
	// private static final String SOAP_ACTION = "http://www.quimicasuiza.com/WSAndroid/validarAccesoICA";
	// private static final String NAMESPACE = "http://www.quimicasuiza.com/WSAndroid";
	// private static final String URL = "http://200.31.110.42/WSAndroid/ServicioDatosUsuario.asmx";

	public static SoapObject GetSoapObject(String MethodName)
	{
		return new SoapObject(NAMESPACE, MethodName);
	}

	public static SoapObject InvokeMethod(String URL, String MethodName, String usuario, String pwd, String pwdAdm) throws IOException, XmlPullParserException
	{
		Log.d("QSLogin", usuario + " - " + pwd);

		SoapObject request = GetSoapObject(MethodName);

		request.addProperty("codusu", usuario);
		request.addProperty("pwd", pwd);
		request.addProperty("keyMovile", pwdAdm);

		SoapSerializationEnvelope envelope = GetEnvelope(request);

		return MakeCall(URL, envelope);
	}

	public static List<DatosUsuario> ValidarAcceso(String codusu, String pwd, String pwdAdm) throws IOException, XmlPullParserException
	{
		SoapObject response = InvokeMethod(URL, METHOD_NAME, codusu, pwd, pwdAdm);

		return RetrieveFromSoap(response);
	}

	public static List<DatosUsuario> RetrieveFromSoap(SoapObject soap)
	{
		List<DatosUsuario> clientes = new ArrayList<DatosUsuario>();

		Log.d("QSLogin - RetrieveFromSoap ", soap.getPropertyAsString(0));

		for (int i = 0; i < soap.getPropertyCount(); i++)
		{

			SoapObject pii = (SoapObject) soap.getProperty(i);
			DatosUsuario usuario = new DatosUsuario();

			usuario.id = pii.getProperty(0).toString().trim();
			usuario.CodUsu = pii.getProperty(1).toString().trim();
			usuario.Usuario = pii.getProperty(2).toString().trim();
			usuario.CodSoc = pii.getProperty(3).toString().trim();
			usuario.NomSoc = pii.getProperty(4).toString().trim();

			clientes.add(usuario);
		}

		return clientes;
	}

	public static SoapSerializationEnvelope GetEnvelope(SoapObject Soap)
	{
		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);

		envelope.dotNet = true;
		envelope.setOutputSoapObject(Soap);

		return envelope;
	}

	public static SoapObject MakeCall(String URL, SoapSerializationEnvelope Envelope) throws IOException, XmlPullParserException
	{
		HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);

		androidHttpTransport.call(SOAP_ACTION, Envelope);

		SoapObject response = (SoapObject) Envelope.getResponse();

		Log.d("QSLogin - MakeCall ", "" + response.getPropertyCount());

		return response;
	}
}
